package com.hehui.module.customer.service.impl;

import com.hehui.module.customer.dao.BaseCustomerMapper;
import com.hehui.module.customer.dao.CustomerTrademarkMapper;
import com.hehui.module.customer.entity.BaseCustomer;
import com.hehui.module.customer.service.ICustomerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CustomerServiceImpl implements ICustomerService {


    private static final Logger log = LoggerFactory.getLogger(ICustomerService.class);

    @Autowired
    private BaseCustomerMapper customerMapper;
    @Autowired
    private CustomerTrademarkMapper customerTrademarkMapper;

    /*查询客户列表*/
    @Override
    public List<BaseCustomer> selectCustomerList(BaseCustomer baseCustomer) {
        return customerMapper.selectAllList(baseCustomer);
    }

    /*根据ID查询客户信息*/
    @Override
    public BaseCustomer selectByPrimaryKey(Long customerId) {
        BaseCustomer customer = customerMapper.selectByPrimaryKey(customerId);
        //查询关联的品牌
        List<Long> trademarks = customerTrademarkMapper.selectTrademarkId(customerId, null);
        if (trademarks!=null){
            customer.setTrademarks(trademarks);
        }
        return customer;
    }

    /*添加用户*/
    @Override
    public int insertCustomer(BaseCustomer baseCustomer) {
        return customerMapper.insertSelective(baseCustomer);
    }

    /*修改用户*/
    @Override
    public int updateCustomer(BaseCustomer baseCustomer) {
        //如果为空 关系不发生变化，如果不为空表示 关系发生变化
        if (baseCustomer.getTrademarks() != null) {
            //删除之前的关系
            customerTrademarkMapper.delete(baseCustomer.getCustomerId(), baseCustomer.getMerchantId());
            if (baseCustomer.getTrademarks().size()>0){
                //添加新的关系
                customerTrademarkMapper.insert(baseCustomer.getCustomerId(), baseCustomer.getMerchantId(), baseCustomer.getTrademarks());
            }
        }
        return customerMapper.updateByPrimaryKeySelective(baseCustomer);
    }

    @Override
    public boolean checkUserNameUnique(String username, Long merchantId) {
        return customerMapper.checkUserNameUnique(username, merchantId).size() == 0 ? true : false;
    }

    @Override
    public boolean checkMobileUnique(String mobile, Long merchantId) {
        return customerMapper.checkMobileUnique(mobile, merchantId).size() == 0 ? true : false;
    }

    @Override
    public boolean checkMailUnique(String mail, Long merchantId) {
        return customerMapper.checkMailUnique(mail, merchantId).size() == 0 ? true : false;
    }

    /*批量修改用户状态*/
    @Override
    public int updateCustomerStateByIds(Long[] customerIds, String state) {
        return customerMapper.updateCustomerStateByIds(customerIds, state);
    }

    /*批量删除用户*/
    @Override
    public int deleteCustomerByIds(Long[] customerIds) {
        return customerMapper.deleteCustomerByIds(customerIds);
    }

    /*生成新的编号*/
    @Override
    public String createNewNum(Long merchantId) {
        String newNum = customerMapper.getMaxNum(merchantId);
        if (newNum == null) {
            newNum = "1000";
        }
        return Integer.parseInt(newNum) + 1 + "";
    }

    @Override
    public boolean checkNumUnique(String num, Long merchantId) {
        return customerMapper.checkNumUnique(num, merchantId).size() == 0 ? true : false;
    }
}
